package org.zky.service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

/**
 * @ClassName: AroundInterceptor
 * @Description: 环绕型通知
 * @author zhaokunyang@utry.cn
 * @date 2015年10月4日 下午7:22:00
 * 
 */
@Component(value = "AroundInterceptor")
public class AroundInterceptor implements MethodInterceptor {

	public String perform(Object object) throws Exception {
		PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(object);
		StringBuffer sb = new StringBuffer();
		for (PropertyDescriptor iterator : pd) {
			sb.append("【");
			sb.append(iterator.getName());
			Method methodGetX = iterator.getReadMethod();

			String value = "";
			try {
				if (!"getClass".equals(methodGetX.getName())) {
					value = (String) methodGetX.invoke(object);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			sb.append(":");
			sb.append(value);
			sb.append("】");
		}
		Logger logger = Logger.getLogger(object.getClass());
		System.currentTimeMillis();
		logger.info("hsql传入参数：【" + sb.toString() + "】");
		return sb.toString();
	}

	public Object performAround(ProceedingJoinPoint pjp, Object object) {

		long start = System.currentTimeMillis();
		PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(object);
		StringBuffer sb = new StringBuffer();
		for (PropertyDescriptor iterator : pd) {
			sb.append("【");
			sb.append(iterator.getName());
			Method methodGetX = iterator.getReadMethod();

			String value = "";
			try {
				if (!"getClass".equals(methodGetX.getName())) {
					value = (String) methodGetX.invoke(object);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			sb.append(":");
			sb.append(value);
			sb.append("】");
		}
		Logger logger = Logger.getLogger(object.getClass());

		logger.info("hsql传入参数：【" + sb.toString() + "】");

		Object retVal = null;
		try {
			retVal = pjp.proceed(new Object[] { object });
		} catch (Throwable e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		logger.info("本次处理用时：" + (end - start) + "毫秒");
		return retVal;

	}

	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("????invoke?");
		return null;
	}

}
